﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Studio.Web.WebControls
{
    public class DropDownListBindable : DropDownList
    {
        protected override void OnDataBinding(EventArgs e)
        {
            try { base.OnDataBinding(e); }

            catch (ArgumentOutOfRangeException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                this.ClearSelection();
                // ListItem li = new ListItem("Value not exists in valuelist", "-99");
                // li.Selected = true;
                // this.Items.Insert(0, li);
                if(this.Items.Count > 0)
                    this.Items[0].Selected = true;
            }
        }

        public override string SelectedValue
        {
            get
            {
                return base.SelectedValue;
            }
            set
            {
                try
                {
                    base.SelectedValue = value;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    this.ClearSelection();
                    // ListItem li = new ListItem("Value not exists in valuelist", "-99");
                    // li.Selected = true;
                    // this.Items.Insert(0, li);
                    if (this.Items.Count > 0)
                        this.Items[0].Selected = true;
                }
            }
        }
    }
}
